// import CryptoJS from 'crypto-js'

/**
 * @author: wangyuhong
 * @date:
 * @desc: 深拷贝
 * @lastEditor:
 */
export function deepClone (obj, toemptystr = false) {
  let copy
  // handle null undefined to empty string
  if (toemptystr && (obj === null || obj === undefined)) return ''

  // Handle the 3 simple types, and null or undefined
  if (null == obj || 'object' != typeof obj) return obj
  // Handle Date
  if (obj instanceof Date) {
    copy = new Date()
    copy.setTime(obj.getTime())
    return copy
  }
  // Handle Array
  if (obj instanceof Array) {
    copy = []
    for (var i = 0, len = obj.length; i < len; i++) {
      copy[i] = deepClone(obj[i], toemptystr)
    }
    return copy
  }
  // Handle Function
  if (obj instanceof Function) {
    copy = function () {
      return obj.apply(this, arguments)
    }
    return copy
  }
  // Handle Object
  if (obj instanceof Object) {
    copy = {}
    for (var attr in obj) {
      if (obj.hasOwnProperty(attr)) copy[attr] = deepClone(obj[attr], toemptystr)
    }
    return copy
  }
  throw new Error('Unable to copy obj as type isn\'t supported ' + obj.constructor.name)
}

/**
 * 生成uuid 唯一标示
 * @returns {string}
 */
export function getUUID () {
  var len = 32 //32长度
  var radix = 16 //16进制
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(
    ''
  )
  var uuid = [],
    i
  radix = radix || chars.length
  if (len) {
    for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]
  } else {
    var r
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
    uuid[14] = '4'
    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | (Math.random() * 16)
        uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r]
      }
    }
  }
  return uuid.join('')
}

/**
 * @author: wangyuhong
 * @date: 2019/10/22
 * @desc:接受事件对象$event,返回当前的xy坐标
 */
export function calcPosition (e) {
  let target = e.target
  let clientX = e.clientX, // 光标X,y位置
    clientY = e.clientY
  let finalX = clientX,
    finalY = clientY + 20
  let position = {
    finalX,
    finalY
  }
  return position
}

/**
 * @author: wangyuhong
 * @date: 2019/10/22
 * @desc:对应两个对象字段替换,
 * 第二个参数对象中的字段替换第一个参数对象
 */
export function replaceAllFiled (o, n) {
  for (var p in n) {
    if (n.hasOwnProperty(p)) {
      o[p] = n[p]
    }
  }
}

/**
 * 数组转树形结构
 * @param data
 * @returns {Array}
 */
export function arrayToTree (data) {
  let result = []
  if (!Array.isArray(data)) {
    return result
  }

  data.forEach(item => {
    delete item.children;
  });
  // debugger;
  let map = {};
  data.forEach(item => {
    map[item.sortId] = item;
  });
  // console.log(map, "map值")
  data.forEach(item => {
    let parent = map[item.sortParentId];
    if (parent) {
      (parent.children || (parent.children = [])).push(item);
    } else {
      result.push(item);
    }
  });
  return result;
}


// 图片转base64
export const getBase64 = function (file) {
  return new Promise(function (resolve, reject) {
    let reader = new FileReader()
    let imgResult = ''
    reader.readAsDataURL(file)
    reader.onload = function () {
      imgResult = reader.result
    }
    reader.onerror = function (error) {
      reject(error)
    }
    reader.onloadend = function () {
      resolve(imgResult)
    }
  })
}
